package com.stx.blog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.blog.dao.BlogDAO;
import com.stx.blog.entity.BlogEntity;

import com.stx.blog.vo.BlogVO;

import com.stx.category.entity.CategoryEntity;

public class BlogDAOImpl extends JdbcDaoSupport implements BlogDAO {

	class CategoryRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			CategoryEntity categoryEntity = new CategoryEntity();

			categoryEntity.setCid(rs.getString("cid"));
			categoryEntity.setCategoryName(rs.getString("cName"));
			categoryEntity.setUserID(rs.getString("userID"));
			categoryEntity.setCreateTime(rs.getString("createTime"));

			return categoryEntity;
		}

	}

	class BlogRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			BlogEntity blogEntity = new BlogEntity();
			blogEntity.setBlogID(rs.getString("bid"));
			blogEntity.setUserID(rs.getString("userid"));
			blogEntity.setTitle(rs.getString("title"));
			blogEntity.setBlogContent(rs.getString("blogContent"));
			blogEntity.setCid(rs.getString("cid"));
			blogEntity.setReadLevel(rs.getString("readLevel"));
			blogEntity.setReadAble(rs.getString("readAble"));
			blogEntity.setCreateTime(rs.getString("createTime"));
			blogEntity.setDelflag(rs.getString("delflag"));
			blogEntity.setCategoryName(rs.getString("cname"));

			return blogEntity;
		}

	}

	@Override
	public List<BlogEntity> listBlog(String userID) {
		// String sql = "select * from bloggertable where userid='" + userID +
		// "'";

		String sql = "select * from bloggertable b,categorytable c where b.userid= '"
				+ userID + "' and b.cid=c.cid";

		JdbcTemplate jt = getJdbcTemplate();

		List<BlogEntity> blogList = jt.query(sql, new BlogRowMapper());

		return blogList;

	}

	@Override
	public List<BlogEntity> getBlogByUserID(String userID) {
		return null;

	}

	@Override
	public BlogEntity getBlogByBid(String bid) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql = "select * from bloggertable b , categorytable c where bid=? and b.cid=c.cid";
		Object[] args = { bid };
		BlogEntity blogEntity = new BlogEntity();
		blogEntity = (BlogEntity) jt.queryForObject(sql, args,
				new BlogRowMapper());

		return blogEntity;
	}

	@Override
	public void addBlog(BlogVO blogvo) {

		Date date = new Date(new Date().getTime());
		String currentTime = date.getYear() + 1900 + "/"
				+ (date.getMonth() + 1) + "/" + date.getDate();
		blogvo.setCreateTime(currentTime);
		String sql = "insert into bloggertable values(bid.nextval,?,?,?,?,1,1,?,1)";

		Object[] args = { blogvo.getUserID(), blogvo.getTitle(),
				blogvo.getBlogContent(), blogvo.getCid(),
				blogvo.getCreateTime() };
		JdbcTemplate jt = getJdbcTemplate();
		jt.update(sql, args);

	}

	@Override
	public void deleteBlog(String bid) {
		String sql = "delete from bloggertable where bid=?";
		Object[] args = { bid };

		JdbcTemplate jt = getJdbcTemplate();

		jt.update(sql, args);
	}

	@Override
	public void editBlog(BlogVO blogvo) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql = "update bloggertable set title= ?,blogContent=?,cid=?  where bid = ?";
		Object[] args = { blogvo.getTitle(), blogvo.getBlogContent(),
				blogvo.getCid(), blogvo.getBlogID() };

		jt.update(sql, args);
	}

	@Override
	public List<CategoryEntity> listCatgoryToAddBlog() {
		String sql = "select * from categorytable";

		JdbcTemplate jt = getJdbcTemplate();

		List<CategoryEntity> categoryList = jt.query(sql,
				new CategoryRowMapper());

		return categoryList;

	}

	@Override
	public CategoryEntity getCategoryEntityByCid(String cID) {
		String sql = "select * from categorytable where cid = ?";

		JdbcTemplate jt = getJdbcTemplate();

		Object[] args = { cID };

		CategoryEntity categoryEntity = (CategoryEntity) jt.queryForObject(sql,
				new CategoryRowMapper());

		return categoryEntity;
	}

	@Override
	public List<BlogEntity> listAllBlog() {
		String sql = "select * from bloggertable b,categorytable c where  b.cid=c.cid";

		JdbcTemplate jt = getJdbcTemplate();

		List<BlogEntity> blogList = jt.query(sql, new BlogRowMapper());

		return blogList;
	}

}
